package cn.codewen.shop.servlet;

import cn.codewen.shop.pojo.Category;
import cn.codewen.shop.service.CategoryService;
import cn.codewen.shop.service.impl.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/cate/*")
public class CategoryServlet extends BaseServlet {


    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        //1. 接收参数
        //2. 处理数据
        CategoryService categoryService = new CategoryServiceImpl();
        List<Category> categories = categoryService.findAll();
        //3. 转发或重定向
        request.setAttribute("categories", categories);
        request.getRequestDispatcher("/admin/cate_list.jsp").forward(request, response);

    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        if (request.getMethod().equals("POST")) {
            //1.接收参数
            String name = request.getParameter("name");
            String status = request.getParameter("status");
            String sort = request.getParameter("sort");
            //2.处理数据
            //2.1 创建 category对象
            Category category = new Category();
            //2.2 给category赋值
            category.setName(name);
            category.setStatus(Integer.parseInt(status));
            category.setSort(Integer.parseInt(sort));

            //2.3 创建categoryService对象
            CategoryServiceImpl categoryService = new CategoryServiceImpl();
            //2.4 调用insert方法
            categoryService.insert(category);
            //3.转发或跳转
            response.sendRedirect("/admin/cate/list");
        } else {

            //1.接收参数
            //2.获取数据
            //3.转发或跳转
            request.getRequestDispatcher("/admin/cate_add.jsp").forward(request, response);
        }

    }

    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        if (request.getMethod().equals("POST")) {
            //1.接收参数
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String status = request.getParameter("status");
            String sort = request.getParameter("sort");
            //2.处理数据
            //2.1 创建 category对象
            Category category = new Category();
            //2.2 给category赋值
            category.setId(Integer.parseInt(id));
            category.setName(name);
            category.setStatus(Integer.parseInt(status));
            category.setSort(Integer.parseInt(sort));

            //2.3 创建categoryService对象
            CategoryServiceImpl categoryService = new CategoryServiceImpl();
            //2.4 调用insert方法
            categoryService.save(category);
            //3.转发或跳转
            response.sendRedirect("/admin/cate/list");
        } else {
            //1.接收参数
            String id = request.getParameter("id");
            //2.获取数据
            //2.1 创建categoryService对象
            CategoryServiceImpl categoryService = new CategoryServiceImpl();
            //2.2 调用getCateById得到category对象
            Category category = categoryService.getCateById(Integer.parseInt(id));
            //3.转发或跳转
            //3.1 传达数据
            request.setAttribute("category", category);
            //3.2 转发
            request.getRequestDispatcher("/admin/cate_edit.jsp").forward(request, response);
        }

    }

    public void del(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.接收参数
        String id = request.getParameter("id");
        //2.获取数据
        //2.1 创建categoryService对象
        CategoryServiceImpl categoryService = new CategoryServiceImpl();
        //2.2 调用getCateById得到category对象
        categoryService.delete(Integer.parseInt(id));
        //3.转发或跳转
        //3.1 传达数据

        //3.2 转发
        response.sendRedirect("/admin/cate/list");
    }
}
